/**
 * @author Alfred
 * @note 用户类，进行用户各种操作
 */

package team.wh1tegive.user;

import org.apache.commons.codec.digest.DigestUtils;
import team.wh1tegive.db.DatabaseAction;

import java.util.ArrayList;

public class User extends UserAction {
    public User(String uname) {
        super(uname);
    }

    public boolean pwdUpdate(String pwd) {
        String token = DigestUtils.md5Hex(uname + pwd);
        String sql = "UPDATE User SET upwd = \"" + token + "\" WHERE uid = " + uid + ";";
        int i = DatabaseAction.executeUpdate(sql);
        return i == 1;
    }



    public boolean cashChange(CashFlow cf) {
        boolean isInCome = cf.isInCome.equals("1");
        String sql = String.format("INSERT INTO UserAction VALUES (%d, %s, %s, %s, %s, %.2f);", uid, cf.actionId, cf.date, cf.isInCome, cf.info, cf.amount);
        int result = DatabaseAction.executeUpdate(sql);
        if (result == 1) {
            if (isInCome) {
                return super.storage(cf.amount);
            } else {
                return super.withdraw(cf.amount);
            }
        } else {
            return false;
        }
    }

//    public ArrayList<CashFlow> getIncome() {
//
//    }
}
